﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace AppDialogBox
{
    internal partial class PromptBox : Form
    {
        public PromptBox(string text, string caption, MessageBoxButtons buttons)
        {
            InitializeComponent();

            AddButtons(buttons);

            this.Text = caption;
            this.labelPrompt.Text = text;
        }

        private void PromptBox_Load(object sender, EventArgs e)
        {
            AdjustLayout();
        }

        private void AdjustLayout()
        {
            int difference;

            this.labelPrompt.AutoSize = true;
            this.flowLayoutPanel.AutoSize = true;

            this.flowLayoutPanel.Top = this.labelPrompt.Bottom + 10;
            this.Height = this.labelPrompt.Height + this.flowLayoutPanel.Height + 65;
            this.Width = Math.Max(this.labelPrompt.Width, this.flowLayoutPanel.Width) + 30;

            difference = ( this.flowLayoutPanel.Width - this.labelPrompt.Width ) / 2;
            if ( difference > 0 )
            {
                this.labelPrompt.Left = this.flowLayoutPanel.Left + difference; 
            }
            else
            {
               difference = Math.Abs(difference);
               this.flowLayoutPanel.Left = this.labelPrompt.Left + difference;
            }
            
        }

        private void AddButtons(MessageBoxButtons buttons)
        {
            switch (buttons)
            {
                case MessageBoxButtons.OK:
                    this.flowLayoutPanel.Controls.Add( CreateButton("OK",DialogResult.OK,"okButton") );
                    this.AcceptButton = (Button)this.Controls["okButton"];
                    break;
                case MessageBoxButtons.OKCancel:
                    this.flowLayoutPanel.Controls.Add(CreateButton("Cancel", DialogResult.Cancel, "cancelButton"));
                    this.flowLayoutPanel.Controls.Add( CreateButton("OK", DialogResult.OK, "okButton") );
                    this.AcceptButton = (Button)this.Controls["okButton"];
                    this.CancelButton = (Button)this.Controls["cancelButton"];
                    break;
                case MessageBoxButtons.YesNo:
                    this.flowLayoutPanel.Controls.Add(CreateButton("No", DialogResult.No, "noButton"));
                    this.flowLayoutPanel.Controls.Add(CreateButton("Yes", DialogResult.Yes, "yesButton"));   
                    break;
                case MessageBoxButtons.YesNoCancel:
                    this.flowLayoutPanel.Controls.Add(CreateButton("Cancel", DialogResult.Cancel, "cancelButton"));
                    this.flowLayoutPanel.Controls.Add(CreateButton("No", DialogResult.No, "noButton")); 
                    this.flowLayoutPanel.Controls.Add(CreateButton("Yes", DialogResult.Yes, "yesButton"));                        
                    this.CancelButton = (Button)this.Controls["cancelButton"];
                    break;
            }
        }

        private Button CreateButton(string text, DialogResult dialogResult, string buttonName)
        {
            Button button = new Button();

            button.Text = text;
            button.DialogResult = dialogResult;
            button.Name = buttonName;

            button.SetBounds(10, 10, 65, 30);
            button.BackColor = Color.White;

            return button;
        }
    }
}

